package level.model;

import java.awt.Dimension;
import java.awt.Point;
import java.util.ArrayList;

import level.editor.control.tile.TileConstants;

public class StateCollisionLayer extends StateLayer
{

	public StateCollisionLayer(String name, int zOrder)
	{
		super(name, zOrder);
		collisions = new ArrayList<Collision>();
		dimension = new Dimension();
		topLeft = new Point();
	}

	public ArrayList<Collision> getCollisions()
	{
		return collisions;
	}

	public Dimension getSize()
	{
		return dimension;
	}

	public Point getTopLeft()
	{
		return topLeft;
	}
	
	public boolean isEmpty()
	{
		return collisions.isEmpty();
	}

	public void calculateParameters()
	{
		if (collisions.size() == 0)
		{
			dimension = null;
			topLeft = null;
		}

		else
		{
			int leftMost = 0;
			int rightMost = 0;
			int topMost = 0;
			int bottomMost = 0;
			boolean first = true;

			for (Collision collision : collisions)
			{
				if (first)
				{
					leftMost = (int) Math.min(collision.getLine().getX1(),
							collision.getLine().getX2());
					rightMost = (int) Math.max(collision.getLine().getX1(),
							collision.getLine().getX2());
					topMost = (int) Math.min(collision.getLine().getY1(),
							collision.getLine().getY2());
					bottomMost = (int) Math.max(collision.getLine().getY1(),
							collision.getLine().getY2());
					first = false;

				}
				else
				{
					leftMost = (int) Math.min(
							Math.min(collision.getLine().getX1(), leftMost),
							collision.getLine().getX2());
					rightMost = (int) Math.max(
							Math.max(collision.getLine().getX1(), rightMost),
							collision.getLine().getX2());
					topMost = (int) Math.min(
							Math.min(collision.getLine().getY1(), topMost),
							collision.getLine().getY2());
					bottomMost = (int) Math.max(
							Math.max(collision.getLine().getY1(), bottomMost),
							collision.getLine().getY2());
				}
			}

			dimension.setSize(rightMost - leftMost + 2
					* TileConstants.TILE_SIZE, bottomMost - topMost + 2
					* TileConstants.TILE_SIZE);
			topLeft.x = leftMost - TileConstants.TILE_SIZE;
			topLeft.y = topMost - TileConstants.TILE_SIZE;
		}
	}

	private Dimension dimension;
	private Point topLeft;
	private ArrayList<Collision> collisions;
}
